package io.noties.markwon.action

import android.view.ActionMode
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.widget.AppCompatTextView
import io.noties.markwon.base.R
import timber.log.Timber

class ContextBasedRangeFormattingCallback(val textview: AppCompatTextView) : ActionMode.Callback{

    override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
        mode?.menuInflater?.inflate(R.menu.context_based_range_formatting, menu)
        return true
    }

    override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
       var text = textview.text
        if (text != null) {
            val selectionStart = textview.selectionStart
            val selectionEnd = textview.selectionEnd
            if (selectionStart >= 0 && selectionStart <= text.length) {
                Timber.d("Hide link menu item because the selection is already within a link.")
            } else {
                Timber.d( "SelectionStart is " + selectionStart + ". Expected to be between 0 and " + text.length);
            }
        }
        return false;
    }

    override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
        return false;
    }

    override fun onDestroyActionMode(mode: ActionMode?) {

    }
}